package cn.wen.xframe.monitor;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;

public class MonitorUtil {

    /**
     * <pre>
     * 获取指定JVM内存区域池。
     * 
     * Heap
     * 老年代：Tenured/Old Gen
     * 伊甸园：Eden Space
     * 幸存者：Survivor Space
     * 
     * Non-Heap
     * 方法区：Perm Gen
     * 代码缓存：Code Cache
     * 
     * demo: getMemoryPoolMXBean("Perm Gen") or getMemoryPoolMXBean("Tenured Gen")
     * </pre>
     * 
     * @param area 内存区域名称后缀
     * @return 内存池
     */
    static MemoryPoolMXBean getMemoryPoolMXBean(String area) {
	for (MemoryPoolMXBean memoryPool : ManagementFactory.getMemoryPoolMXBeans()) {
	    // name est "Perm Gen" ou "PS Perm Gen" (32 vs 64 bits ?)
	    if (memoryPool.getName().endsWith(area)) {
		return memoryPool;
	    }
	}
	return null;
    }

}
